package com.example.sixtrainingdemo.adapter;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.view.ActionBarPolicy;
import androidx.recyclerview.widget.RecyclerView;

import com.example.sixtrainingdemo.R;
import com.example.sixtrainingdemo.contract.MainContract;
import com.example.sixtrainingdemo.entity.Goods;
import com.example.sixtrainingdemo.entity.Photo;

import java.util.List;

public class GoodsListAdapter extends RecyclerView.Adapter<GoodsListAdapter.ViewHolder> {

   private List<Goods> mList;
   private MainContract.OnItemClickListener mListener;
   private MainContract.OnItemClickListener mOnItemClickListener;

   public GoodsListAdapter(List<Goods> mGoodsList, MainContract.OnItemClickListener listener){
      mList = mGoodsList;
      mListener = listener;
   }

   public void updateData(List<Goods> list) {
      mList.clear();
      mList = list;
      notifyDataSetChanged();
   }

   @NonNull
   @Override
   public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.goods_item, null);
      ViewHolder viewHolder = new ViewHolder(view);
      return viewHolder;
   }


   public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
      Goods goods = mList.get(position);
      holder.textView1.setText(goods.getName());
      holder.textView2.setText("$ " + goods.getPrice() + "/" + goods.getUnit());


      List<Photo> photoList = goods.getPhotoList();
      if (photoList != null && photoList.size() > 0) {
         byte[] byteArray = photoList.get(0).getData();
         Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
         holder.photo.setImageBitmap(bitmap);
      } else {
         holder.photo.setImageResource(R.mipmap.ic_launcher);
      }


      holder.itemView.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            // 确保正确调用 onItemClick 方法
            ((MainContract.OnItemClickListener) mOnItemClickListener).onItemClick(holder.getAdapterPosition());
         }
      });

   }
   public void setOnItemClickListener(MainContract.OnItemClickListener listener) {
      mOnItemClickListener = listener;
   }

   @Override
   public int getItemCount() {
      return mList.size();
   }

   public class ViewHolder extends RecyclerView.ViewHolder {
      ImageView photo;
      TextView textView1;
      TextView textView2;

      public ViewHolder(@NonNull View itemView) {
         super(itemView);
         photo = itemView.findViewById(R.id.goods_photo);
         textView1 = itemView.findViewById(R.id.goods_name);
         textView2 = itemView.findViewById(R.id.goods_price);
         itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               if (mOnItemClickListener != null) {
                  // 确保getAdapterPosition()调用时适配器未在更新状态
                  mOnItemClickListener.onItemClick(getAdapterPosition());
               }
            }
         });
      }
               public void bind ( final Goods goods){
                  // 绑定数据到视图组件

                  // 设置点击事件
                  itemView.setOnClickListener(new View.OnClickListener() {
                     @Override
                     public void onClick(View v) {
                        if (mListener != null) {
                           mListener.onItemClick(getAdapterPosition());
                        }
                     }
                  });
               }
   }
}
